package com.xianyun.common.core.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.xianyun.common.core.constant.Constants;

/**
 * 响应信息主体
 *
 * @author XianYun
 */
public class Response<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = Constants.SUCCESS;

    /** 失败 */
    public static final int FAIL = Constants.FAIL;

    private int code;

    private String msg;

    private T data;

    private long total = 0L;

    private boolean more;

    private Map<String, Object> fields;

    public static <T> Response<T> ok()
    {
        return restResult(null, SUCCESS, null,false,0L,null);
    }

    public static <T> Response<T> ok(T data)
    {
        return restResult(data, SUCCESS, null,false,0L,null);
    }

    public static <T> Response<T> ok(T data, String msg)
    {
        return restResult(data, SUCCESS, msg,false,0L,null);
    }
    public static <T> Response<T> ok(T data, Long total)
    {
        return restResult(data, SUCCESS, null,false,total,null);
    }

    public static <T> Response<T> ok(T data, Long total, boolean more)
    {
        return restResult(data, SUCCESS, null,more,total,null);
    }

    public static <T> Response<T> ok(T data, String msg, Long total, boolean more)
    {
        return restResult(data, SUCCESS, msg,more,total,null);
    }

    public static <T> Response<T> fail()
    {
        return restResult(null, FAIL, null,false,0L,null);
    }

    public static <T> Response<T> fail(String msg)
    {
        return restResult(null, FAIL, msg,false,0L,null);
    }

    public static <T> Response<T> fail(T data)
    {
        return restResult(data, FAIL, null,false,0L,null);
    }

    public static <T> Response<T> fail(T data, String msg)
    {
        return restResult(data, FAIL, msg,false,0L,null);
    }

    public static <T> Response<T> fail(int code, String msg)
    {
        return restResult(null, code, msg,false,0L,null);
    }
    public static <T> Response<T> fail(String msg, Map<String,Object> fields)
    {
        return restResult(null, FAIL, msg,false,0L,fields);
    }

    public static <T> Response<T> fail(T data, Map<String,Object> fields)
    {
        return restResult(data, FAIL, null,false,0L,fields);
    }

    public static <T> Response<T> fail(T data, String msg, Map<String,Object> fields)
    {
        return restResult(data, FAIL, msg,false,0L,fields);
    }

    public static <T> Response<T> fail(int code, String msg, Map<String,Object> fields)
    {
        return restResult(null, code, msg,false,0L,fields);
    }

    private static <T> Response<T> restResult(T data, int code, String msg, boolean more, Long total, Map<String,Object> fields)
    {
        Response<T> apiResult = new Response<>();
        apiResult.setCode(SUCCESS);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setTotal(total);
        apiResult.setMore(more);
        if (null==fields){
            fields=new HashMap<>(2);
            if(code==FAIL){
                fields.put("success",false);
                fields.put("msg",msg);
            }
            if (code==SUCCESS){
                fields.put("success",true);
                fields.put("msg",msg);
            }
        }
        apiResult.setFields(fields);
        return apiResult;
    }

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }

    public long getTotal() {
        return total;
    }


    public void setTotal(long total) {
        this.total = total;
    }

    public boolean isMore() {
        return more;
    }

    public void setMore(boolean more) {
        this.more = more;
    }

    public Map<String, Object> getFields() {
        return fields;
    }

    public void setFields(Map<String, Object> fields) {
        this.fields = fields;
    }
}
